package org.csu.springjpetstore.controller;

import com.alipay.api.AlipayApiException;
import org.csu.springjpetstore.domain.Account;
import org.csu.springjpetstore.domain.Cart;
import org.csu.springjpetstore.domain.Order;
import org.csu.springjpetstore.persistence.AlipayBean;
import org.csu.springjpetstore.service.Alipay;
import org.csu.springjpetstore.service.CartService;
import org.csu.springjpetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @projectName: SpringJPetstore
 * @package: org.csu.springjpetstore.controller
 * @className: OrderController
 * @author: LuoYuHang
 * @description: TODO
 * @date: 2022/3/13 14:46
 * @version: 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    private Order order;
    private Account account;
    //    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    @Autowired
    private OrderService confirmOrderService;
    @Autowired
    private CartService cartService;


    @Autowired
    Alipay alipay;
    @PostMapping(value = "alipay")
    @ResponseBody
//    String outTradeNo, String subject, String totalAmount, String body
    public String alipay(AlipayBean alipayBean) throws AlipayApiException {
        return alipay.pay(alipayBean);
    }



    @GetMapping("/confirmOrder")
    public String ConfirmOrderController(@Autowired HttpServletRequest request) {
        HttpSession session = request.getSession();
        //PaymentTable BillingTable
        order = (Order) session.getAttribute("order");
        if (order == null) {
            order = new Order();
        }
        account = new Account();
        account = (Account) session.getAttribute("loginUser");
        order.setUsername(account.getUsername());
        System.out.println(account.getUsername() + "测试测试测试测试测试测试测试测试测试测试测试测试测试");
        order.setCardType(request.getParameter("order.cardType"));
        System.out.println(request.getParameter("order.cardType") + "测试测试测试测试测试测试测试测试测试测试测试测试测试");
        order.setCreditCard(request.getParameter("order.creditCard"));
        System.out.println(request.getParameter("order.creditCard") + "测试测试测试测试测试测试测试测试测试测试测试测试测试" + "  " + request.getParameter("order.shipToFirstName"));
        order.setExpiryDate(request.getParameter("order.expiryDate"));
        order.setBillToFirstName(request.getParameter("order.billToFirstName"));
        order.setBillToLastName(request.getParameter("order.billToLastName"));
        order.setBillAddress1(request.getParameter("order.billAddress1"));
        order.setBillAddress2(request.getParameter("order.billAddress2"));
        order.setBillCity(request.getParameter("order.billCity"));
        order.setBillState(request.getParameter("order.billState"));
        order.setBillZip(request.getParameter("order.billZip"));
        order.setBillCountry(request.getParameter("order.billCountry"));

        session.setAttribute("order", order);

        //ShippingTable
        order = (Order) session.getAttribute("order");
        order.setShipToFirstName(request.getParameter("order.shipToFirstName"));
        order.setShipToLastName(request.getParameter("order.shipToLastName"));
        order.setShipAddress1(request.getParameter("order.shipAddress1"));
        order.setShipAddress2(request.getParameter("order.shipAddress2"));
        order.setShipCity(request.getParameter("order.shipCity"));
        order.setShipState(request.getParameter("order.shipState"));
        order.setShipZip(request.getParameter("order.shipZip"));
        order.setShipCountry(request.getParameter("order.shipCountry"));
        session.setAttribute("order", order);

        return "order/ConfirmOrder";
//        cart = null;
//        session.setAttribute("cart", cart);
    }

    @GetMapping("/ListOrders")
    public String ListOrdersController(@Autowired HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("loginUser");
        if (account == null) {
            request.setAttribute("msg", "请先登录");
            return "account/SignOnForm";

        }
        List<Order> orderList = confirmOrderService.getConfirmByusername(account.getUsername());
        if (orderList == null) {
            request.setAttribute("message", "订单为空");
            return "order/ListOrders";
        } else {
            session.setAttribute("orderList", orderList);
            return "order/ListOrders";

        }

    }

    @GetMapping("/viewConfirmOrder")
    public String NewOrderFromController(@Autowired HttpServletRequest request) {

        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("loginUser");
        Cart cart = (Cart) session.getAttribute("cart");
        Order order = new Order();
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        order = confirmOrderService.getConfirmByOrderID(orderId);
//    orderDAO.getOrderByOrderId(orderId);
//        session.setAttribute("order", order);
//        request.getRequestDispatcher(VIEW_ORDER).forward(request, response);
        // TODO: 2022/3/15
//        ShoppingCartDAO shoppingCartDAO = new ShoppingCartDAOImpl();
//        shoppingCartDAO.store(account.getUsername(), cart.getCartItemList());
        return "/order/ConfirmOrder";
    }

    @GetMapping("/NewOrderForm")
    public String NewOrderForm(@Autowired HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("loginUser");
        Cart cart = (Cart) session.getAttribute("cart");
//        Order order = new Order();
//         List<Order> orders=confirmOrderService.getConfirmByusername(account.getUsername());
        if (account == null) {

            request.setAttribute("msg", "请先登录");
            return "account/SignOnForm";

        } else if (cart != null) {
            //order.initOrder(account, cart);
//            session.setAttribute("order", orders);
            return "order/NewOrderForm";
        } else {
            request.setAttribute("message", "购物车为空");
            return "common/Error";
        }
    }


    @GetMapping("ViewOrder")
    public String ViewOrder(@Autowired HttpServletRequest request) {

        HttpSession session = request.getSession();

        order = (Order) session.getAttribute("order");
        order.setStatus("OK");
        order.setOrderDate(new Date());//java.util.Date 无参数默认系统当前时间
        account = new Account();
        account = (Account) session.getAttribute("loginUser");
        Cart cart = (Cart) session.getAttribute("cart");
        order.setTotalPrice(cart.getSubTotal());

        confirmOrderService.insertOrders(order);
        session.setAttribute("order", order);
        cart = null;
        session.setAttribute("cart", cart);
        cartService.deleteInCart1(account.getUsername());

        return "order/ViewOrder";


    }


//    @GetMapping("/ShippingFrom")
//    public String ShippingFromController(@Autowired HttpServletRequest request){
//        HttpSession session = request.getSession();
//        order = (Order) session.getAttribute("order");
//
//        if (order == null) {
//            order = new Order();
//        }
//        account = new Account();
//        account = (Account) session.getAttribute("loginUser");
//        order.setUsername(account.getUsername());
//        order.setCardType(request.getParameter("order.cardType"));
//        order.setCreditCard(request.getParameter("order.creditCard"));
//        order.setExpiryDate(request.getParameter("order.expiryDate"));
//        order.setBillToFirstName(request.getParameter("order.billToFirstName"));
//        order.setBillToLastName(request.getParameter("order.billToLastName"));
//        order.setBillAddress1(request.getParameter("order.billAddress1"));
//        order.setBillAddress2(request.getParameter("order.billAddress2"));
//        order.setBillCity(request.getParameter("order.billCity"));
//        order.setBillState(request.getParameter("order.billState"));
//        order.setBillZip(request.getParameter("order.billZip"));
//        order.setBillCountry(request.getParameter("order.billCountry"));
//
//        session.setAttribute("order", order);
//
//        return null;
//    }
}
